﻿using Autofac.Features.ResolveAnything;
using Autofac;

namespace DockerWeb
{

    public class AutofacHelper : Autofac.Module
    {
        //注册分类
        protected override void Load(ContainerBuilder builder)
        {
            //所有继承ITestServiceA的都注册了
            builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(t => t.IsAssignableTo<IService>()));
            //var assemblys = RuntimeHelper.GetAllAssemblies().ToArray();
            //builder.RegisterAssemblyTypes(assemblys)
            //.SingleInstance()
            //.AsImplementedInterfaces()
            //.EnableInterfaceInterceptors();
            //var assemblies = new[] { Assembly.GetExecutingAssembly() };

            //builder.RegisterAssemblyTypes(assemblies)
            //    .Where(t => typeof(IService).IsAssignableFrom(t))
            //    .SingleInstance()
            //    .AsImplementedInterfaces();

            //builder.RegisterAssemblyTypes(assemblies)
            //    .Where(t => typeof(IStrategy).IsAssignableFrom(t))
            //    .AsImplementedInterfaces();

            //builder.RegisterAssemblyTypes(assemblies)
            //    .Where(t => typeof(IViewModel).IsAssignableFrom(t) && !typeof(ITransientViewModel).IsAssignableFrom(t))
            //    .AsImplementedInterfaces();

            //// several view model instances are transitory and created on the fly, if these are tracked by the container then they
            //// won't be disposed of in a timely manner

            //builder.RegisterAssemblyTypes(assemblies)
            //    .Where(t => typeof(IViewModel).IsAssignableFrom(t))
            //    .Where(t =>
            //    {
            //        var isAssignable = typeof(ITransientViewModel).IsAssignableFrom(t);
            //        if (isAssignable) Debug.WriteLine("Transient view model - " + t.Name);

            //        return isAssignable;
            //    })
            //    .AsImplementedInterfaces()
            //    .ExternallyOwned();
            //builder.RegisterAssemblyTypes(assemblys).SingleInstance().AsImplementedInterfaces();
            //builder.RegisterAssemblyTypes().Where(type => typeof(IService).IsAssignableFrom(type) && !type.IsAbstract);
            //builder.RegisterAssemblyTypes(assemblys).Where(t => t.Name.EndsWith("Repository") && !t.Name.StartsWith("I")).AsImplementedInterfaces();
            //builder.RegisterAssemblyTypes(assemblys).Where(t => t.Name.EndsWith("AppService") && !t.Name.StartsWith("I")).AsImplementedInterfaces();
        }
    }
}
